|
|
|
|
|
|
|
One of the intriguing tasks windows programmers sometimes undertake relates to using one program to control the system or another application. The first step in many of these programs is to obtain the window handle for the other application. The FindWindow API function can be used to find a window handle, given its caption or class. The C declaration for FindWindow is as follows: |
|
|
|
|
|
|
|
|
HWND FindWindow( LPCTSTR lpClassName, // pointer to class name
LPCTSTR lpWindowName // pointer to window name
); |
|
|
|
|
|
|
|
|
This function is designed so that you can specify either the window class (the type of window) or its name (the caption). You can also specify both, but that's rarely necessary. Since this example wants to use the window caption, you'll just set the class name to NULL. |
|
|
|
|
|
|
|
|
You could look up the declaration for FindWindow in the api32.txt file (included on the CD with this book) or the win32api.txt file that comes with Visual Basic, but why not try to figure it out first? |
|
|
|
|
|
|
|
|
Since FindWindow is part of the window subsystem, it uses User32 (kernel32 contains core OS functions, and GDI32 contains graphic functions, as described in Tutorial 1, "Finding Functions," in Part III of this book). Since the function takes string parameters, you know that there are both ANSI and Unicode entry points for the function, so you'll need to use an alias to access the ANSI entry point.' An HWND is a handle, which under Win32 is a Long, so the return value is a Long. Both parameters are strings, which need to be passed ByVal so that they will be passed as NULL-terminated strings. The declaration is thus |
|
|
|
|
|
|
|
|
Declare Function FindWindow Lib "User32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As Long) As Long |
|
|
|
 |
|
 |
|
|
1 If you do not already know that API functions that take string parameters have separate entry points, or have no idea what the terms "ANSI," "Unicode," or "entry point" refer to, please take a break from this puzzle and read Tutorial 5, "The ByVal Keyword: The Solution to 90 Percent of All API Problems," in Part III of this book. |
|
|
|
|
|